<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-row>
          <a-col :span="12">
            <a-form-item label="收款编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['relevcode', validatorRules.relevcode]" :disabled="disableSubmit" placeholder="请输入收款编号(关联单号)"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="所属合同" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" :disabled="disableSubmit" v-decorator="['agreementId', validatorRules.agreementId]" :trigger-change="true" dictCode="ClmAgreement,name,id, typeId = Sale" placeholder="请选择类型"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="收款金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['collectionAmount']" :disabled="disableSubmit" placeholder="请输入收款金额"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="单据日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择单据日期" v-decorator="['billdate', validatorRules.billdate]" :disabled="disableSubmit" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
         <a-col :span="12">
            <a-form-item label="财务组织" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag :disabled="disableSubmit" placeholder="请选择财务组织" @change="handleCompanyChange"
                                   v-decorator="['paymentCompany', validatorRules.paymentCompany]"
                                   otherField="dictionaryId"
                                   dict="DataDictionary,description,dictionaryCode, dictionaryTypeId =COSTPAYMENT_TYPE"/>
            </a-form-item>
         </a-col>
         <!-- <a-col :span="12">
           <a-form-item label="收款类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
             <j-dict-select-tag
               :disabled="disableSubmit"
               type="list"
               v-decorator="['typeId', validatorRules.typeId]"
               :trigger-change="true"
               dictCode="DataDictionary,description,dictionaryCode, dictionaryTypeId =COLLECTION_TYPE"
               placeholder="请选择收款类型"
             />
           </a-form-item>
         </a-col> -->
          <a-col :span="12">
            <a-form-item label="部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag :disabled="disableSubmit" placeholder="请选择部门"
                                   v-decorator="['dept', validatorRules.dept]"
                                   :dict="dict"/>
            </a-form-item>
          </a-col>
         <a-col :span="12">
            <a-form-item label="起算日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择起算日期" :disabled="disableSubmit" v-decorator="['busidate', validatorRules.busidate]" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
         </a-col>

          <a-col :span="12">
            <a-form-item label="提交人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list"
                v-decorator="['partyId', validatorRules.partyId]"
                :trigger-change="true" :disabled="disableSubmit"
                dictCode="Person,name,partyId,typeId = EMPLOYEE"
                placeholder="请选择提交人"
              />
            </a-form-item>
          </a-col>


        </a-row>
      </a-form>

      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="发票明细" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :disabled="disableSubmit"
            :ref="refKeys[0]"
            :tableName="refKeys[0]"
            :loading="clmCollectionItemTable.loading"
            :columns="clmCollectionItemTable.columns"
            :dataSource="clmCollectionItemTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="!disableSubmit"/>
        </a-tab-pane>
        
      </a-tabs>

    </a-spin>
  </j-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'  
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
  import { httpAction, getAction } from '@/api/manage'
  export default {
    name: 'ClmCollectionOrderModal',
    mixins: [JEditableTableMixin],
    components: {
      JDate,
      JDictSelectTag,
      JSearchSelectTag,
    },
    data() {
      return {
        dict:'DataDictionary,description,dictionaryId, dictionaryTypeId =COSTPART_TYPE & parentId=COSTPAYMENT_TYPE0',
        disableSubmit:false,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          agreementId: {
            rules: [
              { required: true, message: '请选择合同!'},
            ]
          },
          relevcode: {
            rules: [
              { required: true, message: '请输入收款编号(关联单号)!'},
            ]
          },
          paymentCompany: {
            rules: [
              { required: true, message: '请选择财务组织!'},
            ]
          },
          billdate: {
            rules: [
              { required: true, message: '请输入单据日期!'},
            ]
          },
          busidate: {
            rules: [
              { required: true, message: '请输入起算日期!'},
            ]
          },
          typeId: {
            rules: [
              { required: true, message: '请选择收款类型!'},
            ]
          },
        },
        refKeys: ['clmCollectionItem', ],
        tableKeys:['clmCollectionItem', ],
        activeKey: 'clmCollectionItem',
        // 发票明细
        clmCollectionItemTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '明细收款编号',
              key: 'tsprowid',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '摘要',
              key: 'scomment',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
//            {
//              title: '往来对象',
//              key: 'objtype',
//              type: FormTypes.select,
//              dictCode: 'DataDictionary,description,dictionaryCode, dictionaryTypeId = COSTOBJTYPE2_TYPE',
//              width: '200px',
//              placeholder: '请选择${title}',
//              defaultValue: '',
//              options:[],
//              validateRules: [{ required: true, message: '${title}不能为空' }],
//            },
//            {
//              title: '部门',
//              key: 'dept',
//              type: FormTypes.select,
//              dictCode: 'DataDictionary,description,dictionaryCode, dictionaryTypeId = COSTPART_TYPE & parentId = COSTPAYMENT_TYPE99',
//              width: '200px',
//              placeholder: '请选择${title}',
//              defaultValue: '',
//              options:[],
//              validateRules: [{ required: true, message: '${title}不能为空' }],
//            },

            {
              title: '客户',
              key: 'customer',
              type: FormTypes.select,
              dictCode: 'ClmCompany,companyName,id, typeId=Cusomer&statusId=2&isSyncNc=Y',
              width: '200px',
              placeholder: '请选择${title}',
              defaultValue: '',
              options:[],
              validateRules: [],
            },
            {
              title: '金额',
              key: 'money',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '无税金额',
              key: 'notaxmoney',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{required: true, message: '${title}不能为空'}],
            },
            {
              title: '税率',
              key: 'taxcodeid',
              type: FormTypes.select,
              dictCode: 'DataDictionary,description,dictionaryCode, dictionaryTypeId = COSTRATE_TYPE',
              width: '200px',
              placeholder: '请选择${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '贷方科目',
              key: 'subjcode',
              type: FormTypes.select,
              dictCode: 'DataDictionary,description,dictionaryCode, dictionaryTypeId = COSTACCOUNTT_TYPE',
              width: '200px',
              placeholder: '请选择${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '开票项目',
              key: 'invproject',
              type: FormTypes.select,
              dictCode: 'DataDictionary,description,dictionaryCode, dictionaryTypeId = COSTBILING_TYPE',
              width: '200px',
              placeholder: '请选择${title}',
              defaultValue: '',
              validateRules: [],
            },
          ]
        },
        url: {
          add: "/online/special/api/createClmCollectionOrder",
          edit: "/online/special/api/updateClmCollectionOrder",
          getEnumId: '/online/dict/getDictItems/',
          clmCollectionItem: {
            list: "/online/api/viewClmCollectionItemByOrderId",
            deleteBatch: "/online/api/deleteClmCollectionItemByOrderBatch",
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      async editAfter() {
        let fieldval = pick(this.model,'typeId','relevcode','dept','collectionAmount','paymentCompany','agreementId','partyId','corpcode','billdate','busidate','billmaker','isSyncNc')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.clmCollectionItem.list, params, this.clmCollectionItemTable)
          if(this.model.paymentCompany){
            let that = this;
            await getAction(this.url.getEnumId+'DataDictionary,description,dictionaryId,dictionaryCode='+this.model.paymentCompany+'&dictionaryTypeId=COSTPAYMENT_TYPE', {otherField:"dictionaryId"}).then(res2 => {
              if(res2.result.length>0){
                let dictionaryId = res2.result[0].dictionaryId;
                that.dict = 'DataDictionary,description,dictionaryId, dictionaryTypeId =COSTPART_TYPE & parentId='+dictionaryId;
              }
            })
          }

        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          ClmCollectionItem: allValues.tablesValue[0].values,
        }
      },
      /** 发起请求，自动判断是执行新增还是修改操作 */
      request(formData) {
        let url = this.url.add, method = 'post'
        if (this.model.id) {
          url = this.url.edit
          method = 'put'
        }
        this.confirmLoading = true
        formData.typeId = 'estimate'
        httpAction(url, formData, method).then((res) => {
          if (res.success) {
            this.$message.success("成功")
            this.$emit('ok')
            this.close()
          } else {
            this.$message.warning(res.message)
          }
        }).finally(() => {
          this.confirmLoading = false
        })
      },
      validateError(msg){
        if(msg){
          this.isHandleOk = false;
        }
        this.$message.error(msg)
      },
      handleCompanyChange(value, backObj) {
        let dictionaryId = backObj.dictionaryId;
        this.dict = 'DataDictionary,description,dictionaryId, dictionaryTypeId =COSTPART_TYPE & parentId='+dictionaryId;
        //this.changePartList(dictionaryId,'Y');
        this.form.setFieldsValue(backObj)
      },
      changePartList(id,isEdit){
        let column = this.clmCollectionItemTable.columns[2];
        column.dictCode ='DataDictionary,description,dictionaryCode, dictionaryTypeId = COSTPART_TYPE & parentId = '+id;
        this.$refs.clmCollectionItem._loadDictConcatToOptions(column)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'typeId','relevcode','dept','collectionAmount','paymentCompany','partyId','corpcode','billdate','busidate','agreementId','billmaker','isSyncNc'))
     },

    }
  }
</script>

<style scoped>
</style>
